<?php
include("check.php");
include("check2.php");
include_once("../function.php");
include_once("../class/email_class.php");
include_once("../class/member_class.php");
include_once("../class/bonus_class.php");
include_once("../class/ulevel_class.php");
include_once("../class/system_class.php");
header("Content-Type: text/html;charset=utf-8");
session_start();

function printTable($_id,$_goodsimg,$_goodsname,$_price){
    echo "<table height=266 border=0 style='width:200px;height:240px;border-left: #0FF solid 1px; border-right: #0FF solid 1px; border-bottom:#0FF solid 1px; border-top:#0FF solid 1px; float:left; margin:8px;'>";
    echo "<tr>";
    echo "<td><a href='goodscontent.php?id=".$_id."' style='text-decoration:none'><img src=../upload/".$_goodsimg." width=200 height=200 border=0></a></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td height=20 align=center><strong>".$_goodsname."</strong></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td height=20>价格：<font color=#FF0000>￥".$_price."</font></td>";
    echo "</tr>";
    echo "</table>";
}
$system=new system_class();
$_sys=$system->system_information(1);
#搜索商品
if ($_POST['Search']){
    $SearchContent=$_POST['SearchContent'];
    if ($SearchContent!=NULL){
        $SearchType=$_POST['SearchType'];
        if ($SearchType==1){
            #搜索商品名称
            $_SESSION['Search']="and goodsname like '%".$SearchContent."%'";
        }
    }else{
        $_SESSION['Search']=NULL;
    }
}else{
    if ($_GET['page']==NULL){
        $_SESSION['Search']=NULL;
        $_SESSION['SearchTime']=NULL;
    }
}

$member=getMemberbyID($_SESSION['ID']);
$ulevel=new ulevel_class();
$_iul=$ulevel->getulevelbyulevel($member['ulevel']);
if($_POST['button3']){
//$ishk=ishk($us['usertel']);
if($us['fenghao']==1){
    echo "<script language=javascript>alert('你已被封号,不能进行此操作.');window.location.href='?'</script>";
}else if($us['ispay']==3){
    echo "<script language=javascript>alert('你尚未回填完成,不能进行此操作.');window.location.href='?'</script>";
}else {
    if ($member['ispay'] == 0) {
        alert("会员未激活，无法购物", "?");
        return;
    }
    if ($_POST['UID'] == "") {
        alert("请选择商品", "?");
        return;
    } else {
        $sumpv = 0;
        $sumjine = 0;
        foreach ($_POST['UID'] as $v) {
            $sql = "select * from guamai where id=" . $v;
            $dan = getOne($sql);
            $sum = $dan['jine'] * $dan['shouxu'];
            $sumjine += $dan['jine'];
            $sumpv += $sum;

        }

        if ($_iul['yl10'] < $member['pv'] + $sumjine) {
            $cha = $_iul['yl10'] - $member['pv'];
            if ($cha < $sumjine) {
                alert("你只能购买" . $cha . "积分", "?");
                return;
            }
        } else {
            $dianzi = $sumpv;
            //实际购买金额
            $shiji = $dianzi * 0.75;
            if ($dianzi * 0.75 <= $member['mey']){//结算
                foreach ($_POST['UID'] as $vf) {
                    $date = date("Y-m-d h:i:s");
                    $rs = getOne("select * from guamai where id={$vf}");//订单信息
                    $goods_update['isgrant'] = 1;
                    $goods_update['bankname'] = $member['nickname'];
                    $goods_update['bdid'] = $member['id'];
                    $goods_update['fdate'] = $date;
                    $goods_update['aixinjijin'] = $rs['jine'] * $rs['shouxu'] * 0.75;
                    $goods_update['yingshi'] = $rs['jine'] * $rs['shouxu']  * 0.75 * 0.1;
                    edit_update_cl('guamai', $goods_update, $rs['id']);
                    //买家更新信息
                    $member_update['mey'] = $member['mey'] - $rs['jine'] * $rs['shouxu']  * 0.75;
                    $member_update['pv'] = $member['pv'] + $rs['jine'] * 0.75;
                    //tongbujine($rs['jine'] * $rs['shouxu']  * 0.85,$member['usertel'],2);
                    edit_update_cl('member', $member_update, $member['id']);

                    $me = getMemberbyID($rs['uid']);//卖家信息
                    $member_update2['cfxf'] = $me['cfxf'] + $rs['jine'] * 0.25*2;//回扣15%
                    $member_update2['mey'] = $me['mey'] - $rs['jine'] * $rs['shouxu']  * 0.75 * 0.1 + $rs['jine'] * $rs['shouxu']  * 0.75;//挂卖手续费
                    $member_update2['maxmey'] = $me['maxmey'] - $rs['jine'] * $rs['shouxu']  * 0.75 * 0.1 + $rs['jine'] * $rs['shouxu']  * 0.75;//
                    $system=new system_class();
                    $_sys=$system->system_information(1);
                    if($_sys['fengding']==2){
                        $member_update2['fengding'] = $me['fengding'] - $rs['jine'] * $rs['shouxu']  * 0.75 * 0.1 + $rs['jine'] * $rs['shouxu']  * 0.75;//
                    }
                    $ssss=getMemberbyID($rs['uid']);
                    $tongbumey=$rs['jine'] * $rs['shouxu']  * 0.75-$rs['jine'] * $rs['shouxu']  * 0.75 * 0.1;
                    //Ftongbujine($tongbumey,$ssss['usertel'],1);
                    edit_update_cl('member', $member_update2, $rs['uid']);
                    $isalert=isfenghao($rs['uid']);
                    if($isalert==0){
                        alert("购买成功", "?");
                    }else{
                        echo "<script language=javascript>alert('你已达到封顶且被封号,请复投');window.location.href='?'</script>";
                    }
                }
            } else {
                alert("奖金不足", "?");
                return;
            }
        }
    }
}
}



?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="css/table.css" rel="stylesheet" type="text/css">
    <link href="css/style.css" rel="stylesheet" type="text/css">
    <title>购物商城</title>
    <SCRIPT LANGUAGE=javascript>
        <!--
        function SelectAll() {
            for (var i=0;i<document.form1.UID.length;i++) {
                var e=document.form1.UID[i];
                e.checked=!e.checked;
            }
        }

        function jiage(numname,pic){
            var price=document.getElementById(pic).value;
            var zong=numname.value*price;
            document.getElementById(pic+"zong").innerText=zong;
            zongjiage=0;
            for (var i=0;i<document.form1.UID.length;i++) {
                var e=document.form1.UID[i];
                if (e.checked==true){
                    var jiage=document.getElementById(e.value).value;
                    var shuangliang=document.getElementById(e.value+"num").value;
                    zongjiage=zongjiage+jiage*shuangliang;
                }
            }
            document.getElementById("zongjiage").innerText=zongjiage;
        }

        function check(objName){
            var o=document.getElementsByName(objName);
            for(i=0;i<o.length;i++)if(o[i].checked)return;
            alert("请选择一种商品");
        }

        function zjiage(){
            zongjiage=0;
            for (var i=0;i<document.form1.UID.length;i++) {
                var e=document.form1.UID[i];
                if (e.checked==true){
                    var jiage=document.getElementById(e.value).value;
                    var shuangliang=document.getElementById(e.value+"num").value;
                    zongjiage=zongjiage+jiage*shuangliang;
                }
            }
            document.getElementById("zongjiage").innerText=zongjiage;
        }
        -->
    </script>
    <style type="text/css">
        *{margin:0; padding:0; font-size:20px;}
        #tablit {margin:0px;width:100%; height:400; border:#BCE2F3 1px solid;padding-top:10px;background:#E4F2FB;}
        #tablit dl{ float:left; width:100%;}
        #tablit dl dt{float:left;border-bottom:#64B8E4 1px solid; width:15px; height:31px; line-height:30px;}
        #tablit dl dd{float:left;width:110px;height:30px; line-height:30px; text-align:center;}
        #tablit .on{
            border:#64B8E4 1px solid;
            border-bottom:#FFF 1px solid;
            color:#FF6600;
            font-weight: bold;
        }
        #tablit .out{
            border:#64B8E4 1px solid;
            color:#000;
            background:#64B8E4;
            font-weight: bold;
        }
        .tabcon{width:99%; height:365px; clear:both;}
        .dis{display:none;}
    </style>
</head>
<body>
<table width="100%" cellpadding="3" cellspacing="1" border="0" align="center" class="table1" style="ma">
    <tr>
        <td align="center">当前价格：<font color="#FF0000">
                <?=$_sys['danjia']?>
            </font></td>
        <td align="center">当前持有量：<font color="#FF0000">
                <?=$member['pv']?>
            </font></td>
    </tr>
    <tr>
        <td align="center">专用币：<font color="#FF0000">
                <?=$member['cfxf']?>
            </font></td>
        <td align="center">当前已卖出：<font color="#FF0000">
                <?=$member['zfmey']?>
            </font></td>

    </tr>
</table>
<br><br><br>
<form name="form1" method="post" action="?">




    <table width="100%" cellpadding="3" cellspacing="1" border="0" align="center" class="table1">
        <tr>
            <td height="30" align="center" >选择</td>
            <td align="center" >卖家</td>
            <td align="center" >价格</td>
            <td align="center" >总量</td>
            <td align="center" >时间</td>
        </tr>
        <?php
        $pagesize = 15; //设置每页记录数
        $sql = "SELECT * FROM `guamai` WHERE isgrant=0 ".$_SESSION['Search']." ".$_SESSION['SearchTime']." ";

        if($query = mysql_query($sql)){
            $sum = mysql_num_rows($query); //计算总记录数
        }else{
            $sum=0;
        }
        if($sum % $pagesize == 0) //计算总页数
            $total = (int)($sum/$pagesize);
        else
            $total = (int)($sum/$pagesize) + 1;
        if (isset($_GET['page'])) //获得页码
        {
            $p = (int)$_GET['page'];
        }
        else
        {
            $p = 1;
        }
        if ($p>$total){
            $p=$total;
        }
        $start = $pagesize * ($p - 1); //计算起始记录
        $sql = "SELECT * FROM `guamai` WHERE  isgrant=0 ".$_SESSION['Search']." ".$_SESSION['SearchTime']." order by id asc limit ".$start.",".$pagesize;

        if($query = mysql_query($sql)){
            while ($row=mysql_fetch_array($query)){
                ?>
                <tr>
                    <td align="center" height="30"><input type="checkbox" name="UID[]" id="UID" value="<?=$row['id']?>" onClick="zjiage();"></td>

                    <td align="center" ><?=$row['username']?></td>
                    <td align="center" ><?=$row['shouxu']?></td>
                    <td align="center" ><?=$row['jine']?></td>
                    <td align="center" ><?=$row['tdate']?></td>
   <!--                 <td align="center" ><input name="<?/*=$row['id']*/?>num" id="<?/*=$row['id']*/?>num" type="text" value="0" size="4" onKeyUp="jiage(this,<?/*=$row['id']*/?>);">          <input name="<?/*=$row['id']*/?>" id="<?/*=$row['id']*/?>" type="hidden" value="<?/*=$row['price']*/?>"></td>
                    <td align="center" ><label name="<?/*=$row['id']*/?>zong" id="<?/*=$row['id']*/?>zong">0</label></td>-->
<!--                    <td align="center">
                        <table>
                            <tr>
                                <td>
                                    <input type="radio" id="dianzi<?php /*echo $row['id'];*/?>" name="fangshi<?php /*echo $row['id'];*/?>" value="1"/><label for="dianzi<?php /*echo $row['id'];*/?>">奖金币</label></td></tr>-->
                            <!--    <tr><td>
        <input type="radio" id="cfxf<?php /*echo $row['id'];*/?>" name="fangshi<?php /*echo $row['id'];*/?>" value="2"/><label for="cfxf<?php /*echo $row['id'];*/?>">创投基金</label></td></tr>-->
                            <!--        <tr><td>
        <input type="radio" id="gwb<?php /*echo $row['id'];*/?>" name="fangshi<?php /*echo $row['id'];*/?>" value="3"/><label for="gwb<?php /*echo $row['id'];*/?>">电子币</label></td></tr>-->
          <!--              </table>
                    <td align="center" ><a href='goodscontent.php?id=<?/*=$row['id']*/?>' style='text-decoration:none'>查看</a></td>-->
                </tr>
                <?php
            }
        }
        ?>
        <tr><td colspan="5"><input name="button3" type="submit" class="button_green" id="button3" value="购买" onClick=" if(confirm('您确定要购买商品吗?')){this.document.selform.submit();return true;}return false;">  </td>
            <td style="display: none">总计金额：<label name="zongjiage" id="zongjiage">0</label></td>
        </tr>
        <tr>
            <td height="21" colspan="10" align="right">
                <table width="100%" border="0">
                    <tr>
                        <td  align="right"><?php echo fenye($p,$pagesize,$sum,$total,$cx)?></td>
                    </tr>
                </table></td>
        </tr>
    </table>
</form>
</body>
</html>